package com.dhcc.bpm.modules.scf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfRecyclingInInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfRecyclingInInfo;
import com.dhcc.bpm.modules.scf.service.IScfRecyclingInInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingInInfoVO.ScfGetRecyclingInDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRecyclingInInfoVO.ScfGetRecyclingInListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 利废企业入库单控制层
 */
@RestController
@AllArgsConstructor
public class ScfRecyclingInInfoController extends BladeController {

    private IScfRecyclingInInfoService scfRecyclingInInfoService;


    /**
     * 查询利废企业入库列表
     *
     * @param scfRecyclingInInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetRecyclingInInfoList")
    // @Cacheable(value = "scfWarehouseListCache", key = "'scfWarehouseListCacheKey-' + #scfWarehouseEntInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetRecyclingInListVO>> pageList(ScfRecyclingInInfoDTO scfRecyclingInInfo, Query query) {
        if (StringUtils.isEmpty( scfRecyclingInInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfRecyclingInInfo.setOrgCodes(  scfRecyclingInInfo.getOrgCode().split(",") );


        if(scfRecyclingInInfo.getOrgCode().contains( "50000000")){
            scfRecyclingInInfo.setOrgCode("50000000");
            scfRecyclingInInfo.setOrgCodes("50000000".split(",") );
        }


        return R.data(scfRecyclingInInfoService.selectScfRecyclingInInfoPage(Condition.getPage(query), scfRecyclingInInfo));
    }

    /**
     * 查询利废企业入库单详情
     *
     * @param scfRecyclingInInfo
     * @return
     */
    @GetMapping("/scfGetRecyclingInInfoDetails")
    // @Cacheable(value = "scfWarehouseDetailsCache", key = "'scfWarehouseDetailsCacheKey-' + #scfWarehouseEntInfo.rowId")
    public R<ScfGetRecyclingInDetailsVO> details(ScfRecyclingInInfoDTO scfRecyclingInInfo) {
        return R.data(scfRecyclingInInfoService.getDetails(scfRecyclingInInfo));
    }

    /**
     * 新增利废企业入库信息
     *
     * @param scfRecyclingInInfo
     * @return
     */
    @PostMapping("scfAddRecyclingInInfoDetails")
    // @CacheEvict(value = "scfWarehouseListCache", allEntries = true)
    public R<Boolean> saveWarehouseEnt(@RequestBody ScfRecyclingInInfoDTO scfRecyclingInInfo) throws Exception {
        return R.status(scfRecyclingInInfoService.saveRecyclingInInfo(scfRecyclingInInfo));
    }

    /**
     * 修改利废企业入库信息
     *
     * @param scfRecyclingInInfo
     * @return
     */
    @PostMapping("/scfModRecyclingInInfoDetails")
    // @CacheEvict(value = "scfWarehouseDetailsCache", key = "'scfWarehouseDetailsCacheKey-' + #scfWarehouseEntIn.rowId")
    public R<Boolean> updateWarehouseEnt(@RequestBody ScfRecyclingInInfoDTO scfRecyclingInInfo) {
        return R.status(scfRecyclingInInfoService.updateRecyclingInInfo(scfRecyclingInInfo));
    }

    /**
     * 批量导入利废入库信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return   导入利废的时候 会生成实时成交明细
     * @throws IOException
     */
    @PostMapping("/scfRecyclingInInfoFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfRecyclingInInfo> scfRecyclingInInfos = scfRecyclingInInfoService.batchSaveRecyclingInInfo(operatorId, file);

        int totalSussess = scfRecyclingInInfoService.bachSaveRecyclingInInfo(scfRecyclingInInfos, operatorId);
        int total = scfRecyclingInInfos.size();
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }


    @PostMapping("/scfRecyclingInInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfRecyclingInInfoService.deleteByIds( split ));
    }
}
